package de.koller.oceanFramework.util;

import java.util.HashMap;
import java.util.Map;

public class GStat {

	private double value = -1;
	private boolean changed = true;
	
	protected final Map<String, Double> additive = new HashMap<>();
	protected final Map<String, Double> mulitply = new HashMap<>();

	public GStat() {
		joinMultiply( "default", 1 );
	}

	public void joinAdd( String identifier, double value ) {
		changed = true;
		
		if( additive.containsKey( identifier ) )
			disposeAdd( identifier );
		
		additive.put( identifier, value );
	}
	
	public void joinMultiply( String identifier, double value ) {
		changed = true;
		
		if( mulitply.containsKey( identifier ) )
			disposeMultiply( identifier );
		
		mulitply.put( identifier, value );
	}
	
	public void disposeAdd( String identifier ) {
		changed = true;
		
		additive.remove( identifier );
	}
	
	public void disposeMultiply( String identifier ) {
		changed = true;
		
		mulitply.remove( identifier );
	}
	
	public double getValue() {
		if( !changed )
			return value;
		
		calculateValue();
		changed = false;
		return value;
	}
	
	private void calculateValue() {
		double m = 0;
		double a = 0;
		
		for( Double d : additive.values() )
			a += d;
		for( Double d : mulitply.values() )
			m += d;
		
		value = a * m;
	}
	
}
